package com.tiger.cloud.controller.crm;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.crm.Peers;
import com.tiger.cloud.service.crm.IPeersService;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 同行信息
 */
@Controller
@RequestMapping(value = "peers")
public class PeersController extends BaseController {

	private static final Logger logger=Logger.getLogger(PeersController.class);
	
	@Autowired
	private IPeersService peersService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		Page<Peers> datas=peersService.page(params);
		return ResponseUtil.ok(datas);
	}
	
	/**
	 * 人脉圈-同行
	 * @param userId
	 * @param peers
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "人脉圈-同行",notes = "人脉圈-同行")
	public Response list(@CurrentUserId Long userId,@RequestBody Peers peers){
		if (peers.getLastUpdateTime()==null) {
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
		}
		peers.setUserId(userId);
		return ResponseUtil.ok(peersService.find(peers));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(@CurrentUserId Long userId,HttpServletRequest request,Peers peers){
		peersService.update(peers);
		return ResponseUtil.ok();
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,HttpServletRequest request,Peers peers){
		peersService.insert(peers);
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,HttpServletRequest request){
		HashMap<String, Object> param=getParamMap(request);
		peersService.remove(param);
		return ResponseUtil.ok();
	}

}